/*
 * @(#)GUIResourceManager.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.resources;

import org.eoti.swing.DefaultMenu;
import org.eoti.awt.ColorUtil;
import org.eoti.util.ResourceManager;

import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.text.SimpleDateFormat;

public class GUIResourceManager
extends ResourceManager
{
	protected static final String COMMA_DELIM = "\\,";

	public GUIResourceManager(String bundleName)
	{
		super(bundleName);
	}

	public GUIResourceManager(String bundleName, Locale locale)
	{
		super(bundleName, locale);
	}

	public GUIResourceManager(ResourceBundle bundle)
	{
		super(bundle);
	}

	public GUIResourceManager(ResourceManager source)
	{
		super(source.getBundle());
	}

	public ActionResourceManager loadActions(String bundleName, String resourceKey, ActionListener listener)
	{
		return new ActionResourceManager(this, bundleName, resourceKey, listener);
	}

	public MenuResourceManager loadMenus(String bundleName, String resourceKey, JMenuBar menuBar, MouseListener mouseListener)
	{
		return new MenuResourceManager(this, bundleName, resourceKey, menuBar, mouseListener);
	}

	public MenuResourceManager loadMenus(String bundleName, String resourceKey, DefaultMenu menu, MouseListener mouseListener)
	{
		return new MenuResourceManager(this, bundleName, resourceKey, menu, mouseListener);
	}

	public ToolbarResourceManager loadTools(String bundleName, String resourceKey, JToolBar toolBar, MouseListener mouseListener)
	{
		return new ToolbarResourceManager(this, bundleName, resourceKey, toolBar, mouseListener);
	}

	public boolean isDefined(String ... values)
	{
		if(values == null) return false;
		if(values.length == 0) return false;
		if(values.length == 1)
		{
			if(values[0].length() == 0) return false;
		}
		return true;
	}

	public Color getColor(String key)
	{
		String[] values = getString(key).split(COMMA_DELIM);
		if(values.length == 3)
		{
			int r = Integer.parseInt(values[0]);
			int g = Integer.parseInt(values[1]);
			int b = Integer.parseInt(values[2]);
			return new Color(r, g, b);
		}else if(values.length == 4){
			int r = Integer.parseInt(values[0]);
			int g = Integer.parseInt(values[1]);
			int b = Integer.parseInt(values[2]);
			int a = Integer.parseInt(values[2]);
			return new Color(r, g, b, a);
		}else if(values.length == 1){
			return ColorUtil.getWebColor(values[0]).getColor();
		}
		throw new IllegalArgumentException(String.format("Unable to determine color for '%s'", key));
	}

	/**
	 * Font could be:
	 * Georgia,BOLD+ITALIC,12
	 * Georgia,BOLD+ITALIC
	 * Georgia,12
	 * Georgia
	 */
	public Font getFont(String key)
	{
		String[] values = getString(key).split(COMMA_DELIM);
		if(values.length == 0)
			throw new IllegalArgumentException(String.format("Unable to determine font for '%s'", key));

		String fontName = values[0];
		int style = Font.PLAIN;
		int size = 12;
		if(values.length == 3)
		{
			style |= ( (values[1].indexOf("BOLD") != -1) ? Font.BOLD : Font.PLAIN);
			style |= ( (values[1].indexOf("ITALIC") != -1) ? Font.ITALIC : Font.PLAIN);
			size = Integer.parseInt(values[2]);
		}else if(values.length == 2){
			try{
				size = Integer.parseInt(values[1]);
			}catch(Exception e){
				style |= ( (values[1].indexOf("BOLD") != -1) ? Font.BOLD : Font.PLAIN);
				style |= ( (values[1].indexOf("ITALIC") != -1) ? Font.ITALIC : Font.PLAIN);
			}
		}
		return new Font(fontName, style, size);
	}

	protected static final String RES_FMT ="%s.%s";
	protected String getString(String fmt, String prefix, String key)
	{
		return getString(String.format(fmt, prefix, key));
	}
	protected String[] getStrings(String fmt, String prefix, String key)
	{
		return getStrings(String.format(fmt, prefix, key));
	}
	protected Boolean getBoolean(String fmt, String prefix, String key)
	{
		return getBoolean(String.format(fmt, prefix, key));
	}

	public SimpleDateFormat getDateTime(String resourceKey)
	{
		return new SimpleDateFormat(getString(resourceKey));
	}
}
